from models.models import User  #引入数据模型
from models.models import StockCollect
from models.models import Strategy
from db_config import db_init as db
# 数据模型封装的SQL查询方法
# https://flask-sqlalchemy.palletsprojects.com/en/2.x/queries/#inserting-records
# 数据表操作的类
class User_Opretion():
    def  __init__(self):
        self.__fields__ = ["id","userName","userPWD","userInfo","userEditTime"]
    
    def _all_user(self):
        #数据库查询.query.all()
        user_list = User.query.all()
        return user_list

    def _reg(self,kwargs):
        #coding：查询用户名是否重复
        #try:
            user_data = User.query.filter_by(userName=kwargs["userName"]).first()
            if user_data :
                return -1
            u = User(**kwargs)
            # 增加一条数据记录
            db.session.add(u)
            db.session.commit()
            user_data = User.query.filter_by(userName=kwargs["userName"]).first()
            return user_data.userID
        # except:
        #     return -1

    def _login(self,username,password):
        try:

            #用户名是否存在
            user_data = User.query.filter_by(userName=username).first()
            db_pwd = user_data.userPWD
            if db_pwd == password:
                return user_data.userID
            else:
                return -1
        except:
            return -1

    def _changePwd(self,username,password):
        try:
            #用户名是否存在
            user_data = User.query.filter_by(userName=username).first()
            # 2.对模型对象的属性进行赋值 (更新数据)
            user_data.userPWD = password
            # 3.提交会话
            db.session.commit()
            return "success"
        except:
            return "fail"


    def _changeInfo(self,username,info):
        #用户名是否存在
        try:
            user_data = User.query.filter_by(userName=username).first()
            user_data.userInfo = info
            db.session.commit()
            return "success"
        except:
            return "fail"


class stock_collect_Operation():
    def  __init__(self):
        self.__fields__ = ["collectID","userID","stockName"]

    def add_collect(self,kwargs):
        sc =  StockCollect(**kwargs)
        db.session.add(sc)
        db.session.commit()
        return "add success"

    def get_collect_stock(self,userID):
        stock_list = StockCollect.query.filter_by(userID=userID)
        return stock_list

    def del_collect(self,kwargs):
        sc = StockCollect.query.filter_by(userID=kwargs['userID'],stockName=kwargs['stockName']).first()
        db.session.delete(sc)
        db.session.commit()
        return "del success"

    #查询id,stockname是否存在数据库内
    def is_collect(self,userID,stockname):
        sc = StockCollect.query.filter_by(userID = userID,stockName = stockname)
        if sc:
            return True
        else:
            return False

#策略数据库操作
class strategy_Operation():
    def  __init__(self):
        self.__fields__ = ["straid","userID","straName","runTime","rollbackTime","code"]

    #增加策略
    def add_strategy(self,kwargs):
        stra = Strategy(**kwargs)
        db.session.add(stra)
        db.session.commit()
        return "add strategy success"
    
    #查询策略
    def get_strategy(self,kwargs):
        strategy_list = Strategy.query.filter_by(userID=kwargs['userID']).all()
        return strategy_list

    #删除策略
    def del_strategy(self,kwargs):
        stra = Strategy.query.filter_by(userID=kwargs['userID'],straName=kwargs['straName']).first()
        db.session.delete(stra)
        db.session.commit()
        return "del strategy success"

    #修改策略代码
    def update_code(self,kwargs):
        stra = Strategy.query.filter_by(userID=kwargs['userID'],straName=kwargs['straName']).first()
        stra.code =  kwargs['code']
        return "code update success"

    #修改策略名
    def update_straName(self,kwargs):
        stra = Strategy.query.filter_by(userID=kwargs['userID'],straName=kwargs['straName']).first()
        stra.straName = kwargs['oldName']
        db.session.commit()
        return "straName update success"

    #修改运行次数
    def update_runtime(self,kwargs):
        stra = Strategy.query.filter_by(userID=kwargs['userID'],straName=kwargs['straName']).first()
        stra.runTime = int(stra.runTime)+1
        db.session.commit()
        return "runTime update success"

    #修改回测次数
    def update_rollbacktime(self,kwargs):
        stra = Strategy.query.filter_by(userID=kwargs['userID'],straName=kwargs['straName']).first()
        stra.rollbackTime = int(stra.rollbackTime)+1
        db.session.commit()
        return "rollbackTime update success"
    
